package com.ihome.common.util;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class IHLogger {
    static {
    	
    	try{
    		ClassLoader loader = IHLogger.class.getClassLoader();
    		String fileName = "resources/log4j.properties";
    		java.net.URL in = null;
    		
    		if(loader != null){
    			in = loader.getResource(fileName);
    		}else{
    			in = ClassLoader.getSystemResource(fileName);
    		}
    		
    		PropertyConfigurator.configure(in.getFile());
    		PropertyConfigurator.configureAndWatch(in.getFile());
    		
    		IHLogger = Logger.getLogger("IHLogger");
    	}catch(Exception e){
    		System.err.println("Configuration file not found error:"+e.getMessage());
    	}
    	
    }
    
    private static Logger IHLogger;
    
    public static final int DEBUG = 1;
    public static final int INFO = 2;
    public static final int WARN = 3;
    public static final int ERROR = 4;
    public static final int FATAL = 5;
    
    public static void logMessage(int Level, String Message){
    	if(IHLogger != null){
    		switch(Level){
    		    case DEBUG:
    		    	IHLogger.debug(Message);
    		    	break;
    		    
    		    case INFO:
    		    	IHLogger.info(Message);
    		    	break;
    		    	
    		    case WARN:
    		    	IHLogger.warn(Message);
    		    	break;
    		    	
    		    case ERROR:
    		    	IHLogger.error(Message);
    		    	break;
    		    	
    		    case FATAL:
    		    	IHLogger.fatal(Message);
    		    	break;
    		}
    	}
    }
    
    public static void logException(Throwable e){
    	
    	if(IHLogger != null)
    		IHLogger.error(e);
    }
    
}
